/**
 * 
 */
package com.jm.web.form;


import com.jm.utils.AttributeBuilder;
import com.jm.utils.StringHelper;
import com.jm.web.form.support.FieldItem;
import com.jm.web.form.support.FormItem;

/**
 * @author yanjie
 *
 * 2011/9/19 下午12:59:25
 */
public class Input extends FieldItem{
	public final static String TAG_ITEMNAME = "input";
	public static String TAG_TYPE = "type";
	protected String strType = "";
	
	/**
	 * @param tag
	 */
	public Input() {}
	
	@Override
	public void init() {
		super.init();
		this.setNodeName(TAG_ITEMNAME);
		if("text".equals(this.getType())){
			this.setStyle(StringHelper.Format(";%1$s;_height:17px;border: "+this.GetExtValue("border", "1")+"px #7F9DB9 solid !important;",this.getStyle()));
			
			//关闭text框默认的自动完成
			this.SetExtValue("autocomplete", "off");
		}
	}
	
	@Override
	public void OnSetProperty(String strName, String strValue) {
		if (strName.compareToIgnoreCase(TAG_TYPE) == 0)
		{
			this.strType = strValue;
			if("hidden".equalsIgnoreCase(this.getType())){
				this.setHidden(true);
			}
			return;
		}
		super.OnSetProperty(strName, strValue);
	}
	
	
	@Override
	public void FillAttributeBuilder(AttributeBuilder attributesBuilder) {
		super.FillAttributeBuilder(attributesBuilder);
		
		if(!StringHelper.IsNullOrEmpty(this.getType()))
			attributesBuilder.Set(TAG_TYPE.toLowerCase(), this.getType());
		
		//处理 checkbox radio 控件的checked属性
		if(this.GetExtValue("checked", false)){
			attributesBuilder.Set("checked", "true");
		}
	}
	
	public String getType() {
		return strType;
	}
	
	public void setType(String strType) {
		this.strType = strType;
	}
	
	public static boolean isInputText(FormItem item){
		if(item == null)
			return false ;
		
		return item instanceof Input && "text".equalsIgnoreCase(((Input)item).getType());
	}
}
